/* 
    Copyright (c) 2011 Microsoft Corporation.  All rights reserved.
    Use of this sample source code is subject to the terms of the Microsoft license 
    agreement under which you licensed this sample source code and is provided AS-IS.
    If you did not accept the terms of the license agreement, you are not authorized 
    to use this sample source code.  For the terms of the license, please see the 
    license agreement between you and Microsoft.
  
    To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604 
  
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;


namespace PivotApp1
{
    public partial class SettingsWithoutConfirmation : PhoneApplicationPage
    {
        private TremoloDataContext TremoloDB;

        public ObservableCollection<UserTable> _tremoloItems;
        public ObservableCollection<UserTable> TremoloItems
        {
            get
            {
                return _tremoloItems;
            }
            set
            {
                if (_tremoloItems != value)
                {
                    _tremoloItems = value;
                    NotifyPropertyChanged("TremoloItems");
                }
            }
        }


        public SettingsWithoutConfirmation()
        {
            InitializeComponent();

            TremoloDB = new TremoloDataContext(TremoloDataContext.DBConnectionString);

            var allUsers = from UserTable ra in TremoloDB.TremoloUserTable select ra;
            TremoloItems = new ObservableCollection<UserTable>(allUsers);

            if (TremoloItems != null)
            {
                listBoxUsers.ItemsSource = TremoloItems;
            }
        
        }

        /// <summary>
        /// Additional setting clicked event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAdditional_Click(object sender, RoutedEventArgs e)
        {
            if (radio_remove.IsChecked == true)
            {
                UserTable r = listBoxUsers.SelectedItem as UserTable;
                TremoloDB.TremoloUserTable.DeleteOnSubmit(r);
                TremoloItems.Remove(r);
                TremoloDB.SubmitChanges();
            }
            else
            {
           
                 UserTable r = null;
                if (radio_add.IsChecked == true)
                {
                    UserTable t = new UserTable { UserName = username.Text, UserPassword=password.Text, UserLevel = 1 };
                    TremoloDB.TremoloUserTable.InsertOnSubmit(t);
                    TremoloDB.SubmitChanges();
                    TremoloItems.Add(t);
                    var allUsers = from UserTable ra in TremoloDB.TremoloUserTable select ra;
                    List<UserTable> all = allUsers.ToList();
                    r = all.Last();
                   
                }
                else
                {
                    r = listBoxUsers.SelectedItem as UserTable;
                }
                if (eventname.Text != "")
                {
                    EventTable newevent = new EventTable { EventName = eventname.Text, Completion = 0, EventDate = DateTime.Today, UserId = r.UserId };
                    TremoloDB.TremoloEventTable.InsertOnSubmit(newevent);
                    TremoloDB.SubmitChanges();
                }
            }

            username.Text = "";
            password.Text = "";
            eventname.Text = "";


            this.NavigationService.Navigate(new Uri("/EventListView.xaml", UriKind.Relative));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
